/*
 * The MIT License
 * Copyright (c) 2011 kc4.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.kc4.util;

import java.io.Serializable;

/**
 * メソッド特定のためのシグネチャ。
 *
 * @author yoichi kikuchi
 * @since 1.0.0
 */
public class MethodSignature implements Serializable {

	private static final long serialVersionUID = 1L;

	private Class<?> clazz = null;

	private String name = null;

	private Class<?>[] args = null;

	/**
	 * メソッドシグネチャを生成します。
	 *
	 * @param clazz クラス。
	 * @param name メソッド名。
	 * @param args 引数タイプ。
	 */
	protected MethodSignature(final Class<?> clazz, final String name, final Class<?>... args) {

		this.setClazz(clazz);
		this.setName(name);
		this.setArgs(args);
	}

	/**
	 * clazz を取得します。
	 *
	 * @return clazz
	 */
	public Class<?> getClazz() {

		return this.clazz;
	}

	/**
	 * clazz を設定します。
	 *
	 * @param clazz clazz オブジェクト。
	 */
	protected void setClazz(final Class<?> clazz) {

		this.clazz = clazz;
	}

	/**
	 * name を取得します。
	 *
	 * @return name
	 */
	public String getName() {

		return this.name;
	}

	/**
	 * name を設定します。
	 *
	 * @param name name オブジェクト。
	 */
	protected void setName(final String name) {

		this.name = name;
	}

	/**
	 * args を取得します。
	 *
	 * @return args
	 */
	public Class<?>[] getArgs() {

		return this.args;
	}

	/**
	 * args を設定します。
	 *
	 * @param args args オブジェクト。
	 */
	protected void setArgs(final Class<?>[] args) {

		this.args = args;
	}

	@Override
	public boolean equals(final Object obj) {

		if (!(obj instanceof MethodSignature)) return false;

		MethodSignature sig = (MethodSignature) obj;

		boolean eq = this.getClazz().equals(sig.getClazz()) &&
			this.getName().equals(sig.getName());

		if (this.getArgs() == null && sig.getArgs() == null) {

			return eq;
		}

		if (this.getArgs() != null && sig.getArgs() != null && this.getArgs().length == sig.getArgs().length) {

			for (int i = 0, len = this.getArgs().length; i < len; i++) {

				eq &= (this.getArgs()[i] == sig.getArgs()[i]);
			}

			return eq;
		}

		return false;
	}

	@Override
	public int hashCode() {

		int hash = this.getClazz().hashCode();
		hash ^= this.getName().hashCode();

		for (int i = 0; i < this.getArgs().length; i++) {

			hash = hash ^ this.getArgs()[i].hashCode() ^ i;
		}

		return hash;
	}
}
